﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

//http://www.west-wind.com:8080/svn/jQuery/trunk/jQueryControls/Support/ClientScriptProxy.cs
[assembly: WebResource("Pixysoft.Framework.Jsons.Resources.jquery-1.2.3.min.js", "application/x-javascript")]
[assembly: WebResource("Pixysoft.Framework.Jsons.Resources.vl.ejs.v4.min.js", "application/x-javascript")]
[assembly: WebResource("Pixysoft.Framework.Jsons.Resources.pixysoft.jquery.ajax.min.js", "application/x-javascript")]
namespace Pixysoft.Framework.Jsons.WebControls
{
    [
    ValidationPropertyAttribute("Text"),
    Designer(typeof(PixysoftJsonDesigner)),
    DescriptionAttribute("Pixysoft Json Framework Library.")
    ]
    public class PixysoftJson : WebControl
    {
        private static string default_js_pixysoft = "Pixysoft.Framework.Jsons.Resources.jquery-1.2.3.min.js";
        private static string default_js_ejs = "Pixysoft.Framework.Jsons.Resources.vl.ejs.v4.min.js";
        private static string default_js_jquery = "Pixysoft.Framework.Jsons.Resources.pixysoft.jquery.ajax.min.js";

        protected override void OnPreRender(EventArgs e)
        {
            if (this.Page != null)
            {
                ClientScriptManager manager1 = this.Page.ClientScript;

                if (!this.RegisterClientScriptResourceInHeader(typeof(PixysoftJson), default_js_pixysoft))
                    manager1.RegisterClientScriptResource(typeof(PixysoftJson), default_js_pixysoft);

                if (!this.RegisterClientScriptResourceInHeader(typeof(PixysoftJson), default_js_ejs))
                    manager1.RegisterClientScriptResource(typeof(PixysoftJson), default_js_ejs);

                if (!this.RegisterClientScriptResourceInHeader(typeof(PixysoftJson), default_js_jquery))
                    manager1.RegisterClientScriptResource(typeof(PixysoftJson), default_js_jquery);
            }
            base.OnPreRender(e);
        }

        private bool RegisterClientScriptResourceInHeader(Type type, string resourceName)
        {
            return this.RegisterClientScriptResourceInHeader(type, resourceName, true);
        }

        private bool RegisterClientScriptResourceInHeader(Type type, string resourceName, bool topOfHeader)
        {
            if (this.Page.Header == null)
                return false;

            // *** Keep duplicates from getting written
            const string identifier = "headerscript_";
            if (HttpContext.Current.Items.Contains(identifier + resourceName))
                return false;
            else
                HttpContext.Current.Items.Add(identifier + resourceName, string.Empty);

            object val = HttpContext.Current.Items["__ScriptResourceIndex"];
            int index = 0;
            if (val != null)
                index = (int)val;

            // *** Retrieve the Resource URL adjusted for MS Ajax, wwScriptCompression or stock ClientScript
            string script = GetClientScriptResourceUrl(typeof(PixysoftJson), resourceName);

            // *** Embed in header
            StringBuilder sb = new StringBuilder(200);

            sb.AppendLine(@"<script src=""" + script + "\" type=\"text/javascript\"></script>\r\n");

            if (this.Page.Header == null)
                throw new InvalidOperationException("Can't add resources to page: missing <head runat=\"server\"> tag in the page.");

            if (topOfHeader)
                this.Page.Header.Controls.AddAt(index, new LiteralControl(sb.ToString()));
            else
                this.Page.Header.Controls.Add(new LiteralControl(sb.ToString()));

            index++;
            HttpContext.Current.Items["__ScriptResourceIndex"] = index;

            return true;
        }

        private string GetClientScriptResourceUrl(Type type, string resourceName)
        {
            return this.Page.ClientScript.GetWebResourceUrl(type, resourceName);
        }


    }
}
